package com.miage.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.miage.bdd.DaoBdd;
import com.miage.bean.Evenement;
import com.miage.bean.Participant;
import com.miage.utils.Constante;

public class DaoParticipant extends DaoBdd {
	
	public static final String TABLE = Constante.PARTICIPANT_TABLE;

	public static final String ATTR_ID = Constante.PARTICIPANT_COLUMN_ID;
	public static final String ATTR_NOM = Constante.PARTICIPANT_COLUMN_NOM;
	public static final String ATTR_PRENOM = Constante.PARTICIPANT_COLUMN_PRENOM;
	public static final String ATTR_MONTANT_CPTE = Constante.PARTICIPANT_COLUMN_MONTANT_CPTE;
	public static final String ATTR_ID_EVENEMENT = Constante.PARTICIPANT_FOREIGNKEY_ID_EVENEMENT;

	public DaoParticipant(Context pContext) {
		super(pContext);
	}
	
	public void ajouter(Participant p) {
		this.open();
		
		ContentValues value = new ContentValues();
		value.put(ATTR_ID, p.getId());
		value.put(ATTR_NOM, p.getNom());
		value.put(ATTR_PRENOM, p.getPrenom());
		value.put(ATTR_MONTANT_CPTE, p.getMontantCpte());
		value.put(ATTR_ID_EVENEMENT, p.getIdEvenement());
		bdd.insert(TABLE, null, value);
		
		this.close();
	}
	
	public void supprimer(Integer id) {
		this.open();
		
		bdd.delete(
				TABLE, 
				ATTR_ID + " = ?", 
				new String[] { String.valueOf(id) }
		);
		
		this.close();
	}
	
	public void modifier(Participant p) {
		this.open();
		
		ContentValues value = new ContentValues();
		value.put(ATTR_NOM, p.getNom());
		value.put(ATTR_PRENOM, p.getPrenom());
		value.put(ATTR_MONTANT_CPTE, p.getMontantCpte());
		value.put(ATTR_ID_EVENEMENT, p.getIdEvenement());
		bdd.update(
				TABLE, 
				value, 
				ATTR_ID + " = ?",
				new String[] { String.valueOf(p.getId()) }
			);
		
		this.close();
	}
	
	public List<Participant> getParticipants() {
		this.open();
		
		List<Participant> listParticipants = new ArrayList<Participant>();
		Cursor cursor = bdd.query(
				TABLE, 
				new String[] { ATTR_ID, ATTR_NOM, ATTR_PRENOM, ATTR_MONTANT_CPTE, ATTR_ID_EVENEMENT },
				null, 
				null, 
				null,
				null, 
				null, 
				null
			);
		
		for (cursor.moveToFirst() ; !cursor.isAfterLast() ; cursor.moveToNext()) {
			listParticipants.add(
					new Participant(
							cursor.getInt(cursor.getColumnIndex(ATTR_ID)),
							cursor.getString(cursor.getColumnIndex(ATTR_NOM)),
							cursor.getString(cursor.getColumnIndex(ATTR_PRENOM)),
							cursor.getFloat(cursor.getColumnIndex(ATTR_MONTANT_CPTE)),
							cursor.getInt(cursor.getColumnIndex(ATTR_ID_EVENEMENT))
					)
			);
		}
		
		this.close();
		cursor.close();
		return listParticipants;
	}
	
	public List<Participant> getParticipantsByEvent(Evenement e) {
		this.open();
		
		List<Participant> listParticipants = new ArrayList<Participant>();
		Cursor cursor = bdd.query(
				TABLE, 
				new String[] { ATTR_ID, ATTR_NOM, ATTR_PRENOM, ATTR_MONTANT_CPTE, ATTR_ID_EVENEMENT },
				ATTR_ID_EVENEMENT + " = ?", 
				new String[] { String.valueOf(e.getId()) }, 
				null,
				null, 
				null, 
				null
			);
		
		for (cursor.moveToFirst() ; !cursor.isAfterLast() ; cursor.moveToNext()) {
			listParticipants.add(
					new Participant(
							cursor.getInt(cursor.getColumnIndex(ATTR_ID)), 
							cursor.getString(cursor.getColumnIndex(ATTR_NOM)),
							cursor.getString(cursor.getColumnIndex(ATTR_PRENOM)),
							cursor.getFloat(cursor.getColumnIndex(ATTR_MONTANT_CPTE)),
							cursor.getInt(cursor.getColumnIndex(ATTR_ID_EVENEMENT))
					)
			);
		}
		
		this.close();
		cursor.close();
		return listParticipants;
	}

	public Integer getNbParticipantsByEvent(Evenement e) {
		return (Integer) this.getParticipantsByEvent(e).size();
	}
	
	public Integer getNbParticipants() {
		return (Integer) this.getParticipants().size();
	}	


	public Participant getParticipantById(Integer id) {
		this.open();
		
		Participant p = null;
		Cursor cursor = bdd.query(
				TABLE, 
				new String[] { ATTR_ID, ATTR_NOM, ATTR_PRENOM, ATTR_MONTANT_CPTE, ATTR_ID_EVENEMENT },
				ATTR_ID_EVENEMENT + " = ?", 
				new String[] { String.valueOf(id) }, 
				null,
				null, 
				null, 
				null
			);
		
		for (cursor.moveToFirst() ; !cursor.isAfterLast() ; cursor.moveToNext()) {
			
					p= new Participant(
							cursor.getInt(cursor.getColumnIndex(ATTR_ID)), 
							cursor.getString(cursor.getColumnIndex(ATTR_NOM)),
							cursor.getString(cursor.getColumnIndex(ATTR_PRENOM)),
							cursor.getFloat(cursor.getColumnIndex(ATTR_MONTANT_CPTE)),
							cursor.getInt(cursor.getColumnIndex(ATTR_ID_EVENEMENT))
					);
			
		}
		
		this.close();
		cursor.close();
		return p;
	}
	
}
